package pers.cz.codegen.template;

import pers.cz.codegen.codegen.CodeGenerator;
import pers.cz.codegen.describe.*;
import pers.cz.codegen.enums.CodeGenerateConfigItem;
import pers.cz.codegen.enums.MethodAnnotationEnum;
import pers.cz.jefconfig.config.JefConfiguration;
import pers.cz.jefconfig.io.Charsets;
import pers.cz.jefconfig.tools.StringUtils;

import java.io.File;
import java.lang.reflect.Modifier;
import java.sql.SQLException;

/**
 * @program: jef-code-generator
 * @description: 信管代码生成
 * @author: Cheng Zhi
 * @create: 2022-08-09 18:32
 **/
public class ImsBeanCodeGenerator extends CodeGenerator {

    @Override
    public void generateCode() throws SQLException {

        // 从配置文件中获取要生成的类模块名
        String s = JefConfiguration.get(CodeGenerateConfigItem.CODE_GEN_CLASSNAME);
        for (String className : StringUtils.split(s,";")) {

            JavaUnit javaUnit = new JavaUnit(className + "Bean");
            javaUnit.setComment(CommentTemplate.classComment);
            javaUnit.setIpackage(getIpackage());
            //IClass iClassParent = new SimpleClass("BaseBusiBean");
            IClass iClassParent = new SimpleClass(this.getClass());
            javaUnit.setParent(iClassParent.getSimpleName());
            javaUnit.addImport("java.util.List");
            javaUnit.addImport("com.ailk.easyframe.web.common.exception.BaseException");
            javaUnit.addImport("com.ailk.ims.common.BaseBusiBean");
            javaUnit.addImport("com.ailk.ims.ims3h.IMS3hBean");
            javaUnit.addImport("com.ailk.openbilling.persistence.imsintf.entity.BaseResponse");
            javaUnit.addAnnotation("@LogParamter");

            JavaField javaField = new JavaField("req");
            javaField.setModifer(Modifier.PRIVATE);
            String fieldName = className + "Req";
            IClass iClassField = new SimpleClass(fieldName);
            javaField.addReturnType(iClassField);

            JavaMethodParamter paramter = new JavaMethodParamter();
            IClass iClass = new SimpleClass("Object...");
            paramter.setParamterType(iClass);

            JavaMethod javaMethodInit = new JavaMethod("init");
            javaMethodInit.addParamter("input", paramter);
            javaMethodInit.addAnnotation(MethodAnnotationEnum.OVERRIDE);
            javaMethodInit.addContent("req = (" + fieldName + ") input[0];");

            JavaMethod javaMethodValidate = new JavaMethod("validate");
            javaMethodValidate.addParamter("input", paramter);
            javaMethodValidate.addAnnotation("@Override");
            javaMethodValidate.addContent("if (req == null) {");
            javaMethodValidate.addContent("throw IMSUtil.throwBusiException(ErrorCodeDefine.COMMON_PARAM_ISNULL, \"CollectionFeeMgntDoCheckRequest\");");
            javaMethodValidate.addContent("}");

            JavaMethod javaMethodCreateMain3hBeans = new JavaMethod("createMain3hBeans");
            javaMethodCreateMain3hBeans.addParamter("input", paramter);
            javaMethodCreateMain3hBeans.addAnnotation("@Override");
            javaMethodCreateMain3hBeans.setReturnType("List<IMS3hBean>");
            javaMethodCreateMain3hBeans.addContent("return null;");

            JavaMethod javaMethodDoBusiness = new JavaMethod("doBusiness");

            javaMethodDoBusiness.addParamter("result", paramter);
            javaMethodDoBusiness.addAnnotation("@Override");
            javaMethodDoBusiness.setReturnType("Object[]");
            javaMethodDoBusiness.addContent("return null;");

            JavaMethod javaMethodCreateResponse = new JavaMethod("createResponse");
            JavaMethodParamter paramterdoBusiness = new JavaMethodParamter();
            IClass iClassdoBusiness = new SimpleClass("Object[]");
            paramterdoBusiness.setParamterType(iClassdoBusiness);
            javaMethodCreateResponse.addParamter("result", paramterdoBusiness);
            javaMethodCreateResponse.addAnnotation("@Override");
            javaMethodCreateResponse.setReturnType("BaseResponse");
            javaMethodCreateResponse.addContent("return null;");

            JavaMethod javaMethodDestroy = new JavaMethod("destroy");
            javaMethodDestroy.addAnnotation("@Override");

            javaUnit.addField(javaField);
            javaUnit.addMethod(javaMethodInit);
            javaUnit.addMethod(javaMethodValidate);
            javaUnit.addMethod(javaMethodCreateMain3hBeans);
            javaUnit.addMethod(javaMethodDoBusiness);
            javaUnit.addMethod(javaMethodCreateResponse);
            javaUnit.addMethod(javaMethodDestroy);

            javaUnit.saveToSrcFolder(new File(getFileFolder()), Charsets.UTF8);
        }
    }

    @Override
    public void enhanceCode() throws Exception {

    }
}
